/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.cip.esymons.tesoreria.bc;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.cip.esymons.tesoreria.be.ProcesoElectoral;
import org.cip.esymons.tesoreria.be.VotacionTranseuntes;

/**
 *
 * @author Gilmar Montoya
 */
@Stateless
public class VotacionTranseuntesFacade implements VotacionTranseuntesFacadeLocal {
    @PersistenceContext
    private EntityManager em;

    public void create(VotacionTranseuntes votacionTranseuntes) {
        em.persist(votacionTranseuntes);
    }

    public void edit(VotacionTranseuntes votacionTranseuntes) {
        em.merge(votacionTranseuntes);
    }

    public void remove(VotacionTranseuntes votacionTranseuntes) {
        em.remove(em.merge(votacionTranseuntes));
    }

    public VotacionTranseuntes find(Object id) {
        try {
            return em.find(VotacionTranseuntes.class, id);
        } catch (Exception e) {
            return null;
        }
    }

    public List<VotacionTranseuntes> findAll() {
        try {
            return em.createQuery("select object(o) from VotacionTranseuntes as o").getResultList();
        } catch (Exception e) {
            return new ArrayList();
        }
    }

    public List<VotacionTranseuntes> listaColegiadosExternosVotantes(ProcesoElectoral eleccion, Integer tipoOrdena){
        try {
            Query q = null;
            StringBuilder sb = new StringBuilder();
            sb.append(" select vot.* from tes_votacion_transeuntes vot ");
            sb.append(" where vot.pel_codigo = ?1");            
           
            if (tipoOrdena != null) {
                switch (tipoOrdena) {
                    case 0:
                        sb.append(" order by vot.vot_numCipExterno");
                        break;
                    case 1:
                        sb.append(" order by vot.vot_apePatExterno");
                        break;
                    
                }
            }
            q = em.createNativeQuery(sb.toString(), VotacionTranseuntes.class);
            q.setParameter(1, eleccion.getCodigo());            
            return q.getResultList();

        } catch (Exception e) {
            return new ArrayList();
        }
    }

}
